↜ Back to index Introduction to Numerical Analysis 2
Lecture a1
Introduction
- Evaluation 評価方法: based on reports (probably 2)
- Self study: about 3 hours/week is expected
- Contact: use my email npozar@se.kanazawa-u.ac.jp
Goals:
Implement code for:
Gaussian elimination ガウスの消去法 A x = b
LU decomposition LU分解 A = L U
Congugate gradient method 共役勾配法
Sparse matrices 疎行列
We will be using Fortran.
Text editor
Emacs
Visual Studio Code: simple modern editor with sane key shortcuts.
Fortran reference (参考書)
- Fortran cheatsheet
- Fortranによるプログラミング超入門
- Fotran 90/95 reference
- Introduction to Programming with Fortran: textbook from Springer
Fortran knowledge (Fortran知識)
basic arithmetic:
1 + 2
,x = sqrt(2.0)
,x**2
, …comments (コメント): Anything on a line after
!
is ignored.program end:
end
printing to standard output (標準出力)
print *, "hello" ! I prefer this write(*,*) "hello"
reading from standard input (キーボード入力)
read *, x, i ! user can input values for x, i ! separated by space, comma (,) or Enter
do
loops (ループ)do i=1,5 print *, "square of ", i, " is ", i**2 end do
if
condition (条件分岐)if (1 < 2) then ! can also be (1 .lt. 2) print *, "true" else print *, "false" end if
variable declaration (変数宣言)
integer i ! integer variable real x ! single precision real variable 単精度実数変数 real(8) y ! double precision real variable 倍精度実数変数
Basic exercises (練習問題)
Here are a few exercises for today. Feel free to discuss the solution with your neighbors.
Exercise 1. Running a Fortran program
Try to run the following program. Make sure to use file extension (拡張子) .f90
. If you use .f
, you will have to enter 6 spaces at the beginning of each line.
print *, "Hello"
end
For example, save it in a file hello.f90
, and then compile and run it as
$ gfortran hello.f90
$ ./a.exe # use ./a.out on macOS or Linux
Hello
Text following $
are commands to be typed on the terminal command line (端末) (cygwin
on Windows). Lines without $
show the output of the commands. Replace ./a.exe
with ./a.out
if you are on macOS or Linux.
Exercise 2. Fun with numbers
What does the following program print? Why?
= 2.5 i if (i == 2.5) then print *, "Equal" else print *, "Math is wrong!" endif end
What about this one?
= 1 x = x / 2 y = 1 / 2 z print *, y print *, z end
And this one?
= 2 x = x**2 S = 3 x print *, S end
Exercise 3.
Solving the quadratic equation
Write a program that finds the solutions x of the quadratic equation ax^2 + bx + c = 0 given a, b and c from the user. Print the solutions, if any, from smaller to larger.
Example. Suppose that the user enters 1 -3 2
, the program should output something like:
x1 = 1
x2 = 2
Intermediate exercises
Here are a few exercises to improve our knowledge of Fortran. The ideas in these exercises are used to build more complicated programs like the one for Gaussian elimination so try to solve them yourself. These also often appear on entrance exams :)
Exercise 4.
Write a program that reads 3 numbers a, b and c and prints them out from the smallest to the largest.
Example: If the user enters 3 1 2
, the program should print 1 2 3
.
Exercise 5.
Write a program that prints the factorial (階乗) n! = 1 \cdot 2 \cdot \cdots \cdot n where n is a number entered by the user.
Example. If the user enters 4
, the program outputs 24
.
Exercise 6.
Write a program that prints the Fibonacci number F_n. The Fibonacci numbers are defined by the iterative formula \begin{aligned} F_0 &= 0\\ F_1 &= 1\\ F_k &= F_{k-1} + F_{k-2}, \qquad k = 2, 3, \ldots \end{aligned} where n is he number entered by the user.
Example. If the user enters 6
, the program should print 8
.
Exercise 7.
Write a program that reads a number n and tests whether it is a prime number (素数).
Example. For n = 5, print
yes
, for n = 6, printno
.Hint.
mod(k, m)
returns the remainder (剰余) of the division (除法) k / m. For examplemod(5, 3)
returns 2.Hint. Recall that you can use the
exit
command to exit a do loop early:do i = 1, 100 print *, i if (i > 20) then exit end if end do print *, "stopped looping" end
Modify the program to print all the prime numbers between 2 and n.
Example. If the user enters
10
, the program should print2 3 5 7
Modify the program to print the first n prime numbers.
Hint. You might want to use the
do while
loop like:= 2 ! lowest possible prime k do while (n > 0) ! check that k is prime ... if (k is prime) then print *, k = n - 1 n end if = k + 1 ! consider next number k end do
Example. If the user enters
5
, the program should print2 3 5 7 11
Find all the prime numbers less than 1000 using the Sieve of Eratosthenes (エラトステネスの篩).